package com.wn.airplane_mode.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wn.airplane_mode.R;
import com.wn.airplane_mode.tool.AirplaneTool;
/**
 * @author wuning
 * @date 2012年4月5日09:30:14
 * */
public class AirplaneModeTimePickerDialog extends AlertDialog
 {

	private final OnTimeSetListener mCallback;
	private TextView title = null;
	private Button btn_hour_d = null,btn_min_d = null;//减少
	private Button btn_hour_a = null,btn_min_a = null;//增加
	private EditText hour_editText = null,min_editText = null;
	private Button btn_set = null,btn_cancel = null;
	
	int hourOfDay;
    int minute;
    
    private AirplaneTool airplaneTool = null;
    
	public AirplaneModeTimePickerDialog(Context context,OnTimeSetListener callBack, int hourOfDay, int minute) {
		super(context);
		this.mCallback = callBack;
		this.hourOfDay = hourOfDay;
		this.minute = minute;
		
		airplaneTool = AirplaneTool.getInstance(context);
		
		View view = LayoutInflater.from(context).inflate(R.layout.time_picker_dialog, null);
		title = (TextView)view.findViewById(R.id.txt_title);
		
		btn_hour_d = (Button)view.findViewById(R.id.btn_hd);
		btn_min_d = (Button)view.findViewById(R.id.btn_md);
		btn_hour_a = (Button)view.findViewById(R.id.btn_ha);
		btn_min_a = (Button)view.findViewById(R.id.btn_ma);
		
		hour_editText = (EditText)view.findViewById(R.id.edtv_hour);
		min_editText = (EditText)view.findViewById(R.id.edtv_min);
		
		btn_set = (Button)view.findViewById(R.id.btn_set);
		btn_cancel = (Button)view.findViewById(R.id.btn_cancel);

		setView(view, 0, 0, 0, 0);
		setListener();
		updateTitle();
	}
	
	private void setListener(){
		
		btn_hour_d.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				hour_editText.requestFocus();
				if(--hourOfDay < 0){
					hourOfDay = 23;
				}
				
				updateTitle();
			}
		});
		btn_hour_a.setOnClickListener(new Button.OnClickListener() {
					
			@Override
			public void onClick(View v) {
				hour_editText.requestFocus();
				if(++hourOfDay > 23){
					hourOfDay = 0;
				}
				
				updateTitle();
				
			}
		});
		btn_min_d.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				min_editText.requestFocus();
				if(--minute < 0){
					minute = 59;
				}
				
				updateTitle();
				
			}
		});
		btn_min_a.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				min_editText.requestFocus();
				if(++minute > 59){
					minute = 0;
				}
				
				updateTitle();
				
			}
		});
		
		hour_editText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
				if(s.length() ==1 && s.charAt(0)>='3' || s.length() ==2 && s.charAt(0)>='2' && s.charAt(1)>='4'){
					hour_editText.setText("");
				}
				//Log.i("AirplaneModeTimePickerDialog", "hour_editText onTextChanged,CharSequence="+s.toString());
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {
				
				//Log.i("AirplaneModeTimePickerDialog", "hour_editText beforeTextChanged,CharSequence="+s.toString());
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(hour_editText.length()>0){
					hourOfDay = Integer.parseInt(hour_editText.getText().toString());
					title.setText(airplaneTool.genTimeString(hourOfDay, minute));
				}
				//Log.i("AirplaneModeTimePickerDialog", "hour_editText afterTextChanged,Editable="+s.toString());
			}
		});
		
		
		min_editText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length() >=1 && s.charAt(0)>='6'){
					min_editText.setText("");
				}
				//Log.i("AirplaneModeTimePickerDialog", "min_editText onTextChanged,CharSequence="+s.toString());
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
				//Log.i("AirplaneModeTimePickerDialog", "min_editText beforeTextChanged,CharSequence="+s.toString());
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(min_editText.length()>0){
					minute = Integer.parseInt(min_editText.getText().toString());
					title.setText(airplaneTool.genTimeString(hourOfDay, minute));
				}
				
				//Log.i("AirplaneModeTimePickerDialog", "min_editText afterTextChanged,Editable="+s.toString());
			}
		});
		
		btn_set.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mCallback.onTimeSet(hourOfDay, minute);
				dismiss();
			}
		});
		btn_cancel.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

	private void updateTitle(){
		
		if(hourOfDay < 10){
			hour_editText.setText("0"+hourOfDay);
		}else{
			hour_editText.setText(""+hourOfDay);
		}
		
		if(minute < 10){
			min_editText.setText("0"+minute);
		}else{
			min_editText.setText(""+minute);
		}
		
		title.setText(airplaneTool.genTimeString(hourOfDay, minute));
		
	}
	
	public interface OnTimeSetListener{
		/**
         *
         * @param hourOfDay The hour that was set.
         * @param minute The minute that was set.
         */
        void onTimeSet(int hourOfDay, int minute);
	}
	
}
